/**
 * 修改数据列
 */
var update_col_url = "/admin.php/article/editColName";              //修改列名
var check_power_url = "/admin.php/data/checkOutFiledPower";         //检查权限
var update_url_name = "/admin.php/data/update_name";                //修改姓名
var update_url_category_id = "/admin.php/data/update_category_id "; //修改分类
var update_url_province = '/admin.php/data/update_province';        //修改省
var update_url_city = '/admin.php/data/update_city';                //修改市
var update_url_county = '/admin.php/data/update_county';            //修改区县
var update_url_phone = '/admin.php/data/update_phone';              //修改电话
var update_url_operator = '/admin.php/data/update_operator';              //修改运营商
var update_url_hometown = '/admin.php/data/update_hometown';              //修改乡镇
var update_url_street = '/admin.php/data/update_street';              //修改详细地址
var update_url_product = '/admin.php/data/update_product';              //修改主营产品
var update_url_identification = '/admin.php/data/update_identification';              //修改表示
var update_url_update_date = '/admin.php/data/update_update_date';              //修改表示
var update_url_describe  = '/admin.php/data/update_describe';              //修改表示
var import_excel  = '/admin.php/article/importData';              //修改表示
var old_data = '';
var data = '' ;
var now_col={}; //当前对象
$('.col-name').focus(function () {
    old_data = $(this).val();
    now_col = $(this)
});
$('.col-name').blur(function () {
    data = $(this).val();
    var id = $(this).attr('field');
    if(old_data != data){
        $.post(update_col_url,{id:id,data:data},function(e){
            if(e.code==0){
                $(this).val(1212);
            }
            obalertp(e);
        },'json')
    }
});

var obalertp = function (data) {

    if (data.code) {
        toast.success(data.msg);
    } else {
       toast.error(err_msg);
    }
};





var field = ''; //当前修改的字段
var id = ''; //当前修改的id
//关闭修改弹出框
function closeUpdate(){
    $('#import-panel').hide();
    $('#update-panel').hide();
    $('#update-panel-category').hide();
    $('#update-panel-province').hide();
    $("#province_ option[value='']").attr("selected","selected")
    $('#update-panel-city').hide();
    $("#city_ option[value='']").attr("selected","selected")
    $('#update-panel-county').hide();
    $("#county_ option[value='']").attr("selected","selected")
}

//保存修改
function saveUpdate(){
    var value = '';
    var url = '';
    //根据field选择提交的url
    switch (field){
        case 'name' :
            url = update_url_name;
            value = $("#update_value").val();
            break;
        case 'category_id':
            url = update_url_category_id;
            value = $("#category_id").val();
            break;
        case 'province':
            url = update_url_province;
            value = $("#province_").val();
            break;
        case 'city':
            url = update_url_city;
            value = $("#city_").val();
            break;
        case 'county':
            url = update_url_county;
            value = $("#county_").val();
            break;
        case 'phone':
            url = update_url_phone;
            value = $("#update_value").val();
            break;
        case 'operator':
            url = update_url_operator;
            value = $("#update_value").val();
            break;
        case 'hometown':
            url = update_url_hometown;
            value = $("#update_value").val();
            break;
        case 'street':
            url = update_url_street;
            value = $("#update_value").val();
            break;
        case 'product':
            url = update_url_product;
            value = $("#update_value").val();
            break;
        case 'identification':
            url = update_url_identification;
            value = $("#update_value").val();
            break;
        case 'update_date':
            url = update_url_update_date;
            value = $("#update_value").val();
            break;
        case 'describe':
            url = update_url_describe;
            value = $("#update_value").val();
            break;
    }
    var send_data = {id:id,data:value,field:field}
    $.post(url,send_data,function(e){
        if(e.code==1){
            alert(e.msg);
            window.location.reload();
        }else{
            alert(e.msg)
        }
    },'json');
}

//检查是否有权限
function isPower(e){
     field = $(e).attr('field');
     id = $(e).attr('data-id');
     var vaule = $(e).html();
     $.post(check_power_url,{field:field},function(data){
        if(data.code==1){
            if(field == 'category_id'){
                var cate = $(e).attr('category');
                $("select option[value="+cate+"]").attr("selected","selected")
                $('#update-panel-category').show();
            }else if(field == 'province'){
                var province_id = $(e).attr('area-id');
                $.get('/admin.php/Widget/getRegionOptions', {upid: 0, select_id: 0, level : 1}, function(result){
                    $("#province_").html(result);
                    $("select option[value="+province_id+"]").attr("selected","selected")
                })
                $('#update-panel-province').show();
            } else if(field == 'city'){
                //获取省，他的兄弟元素
                var province = $($(e).prev()).attr('area-id');
                var city_id = $(e).attr('area-id');
                $.get('/admin.php/Widget/getRegionOptions', {upid: province, select_id: 0, level : 2}, function(result){ $("#city_").html(result);
                $("#city_ option[value="+city_id+"]").attr("selected","selected") });

                $('#update-panel-city').show();
            }else if(field == 'county'){
                var city = $($(e).prev()).attr('area-id');
                var county_id = $(e).attr('area-id');
                $.get('/admin.php/Widget/getRegionOptions', {upid: city, select_id: 0, level : 3}, function(result){
                    $("#county_").html(result);
                    $("#county_ option[value="+county_id+"]").attr("selected","selected") ;
                });
                $('#update-panel-county').show();
            }else{
                $("#update_value").val(vaule);
                $('#update-panel').show();
            }
        }else{
            alert(data.msg)
        }
     },'json');
}
//编辑数据
function edit_data(url){
    window.location.href = url;
}
//导出数据
function export_data(url){
    window.location.href = url;
}
//检查是否有上传文件的权限
function uploadFilePower(filed){
    $.get(check_power_url,{field:filed},function(data){
        $('#import-panel').show();
    })
}

function upload_file(){
    var path = $(".upload-pre-file >a").html();
    console.log(path);
    $.get(import_excel,{path:path},function(data){
        console.log(data)
        if(data.code==1){
            alert(data.msg);
            window.location.reload();
        }else{
            alert(data.msg);
        }
    },'json')
}
